package com.cjzuji.rental.util;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class ErSRRZBillUtil {
//    public static List<Map<String, Object>> getRenRZuBillList(String authorization, String orderId, String cookie) {
//        String url = "https://admin.rrzu.com/v2-bill/index?order_id="+orderId;
//        String html = Tool.getRequest(url,"","", "", cookie);
//        List<Map<String, Object>> result = new ArrayList<>();
//        try {
//            Document doc = Jsoup.parse(html);
//            Element table = doc.selectFirst("table.table.table-bordered");
//
//            if (table != null) {
//                Elements headers = table.select("thead th");
//                List<String> headerList = new ArrayList<>();
//                for (Element header : headers) {
//                    headerList.add(header.text());
//                }
//
//                Elements rows = table.select("tbody tr");
//                for (Element row : rows) {
//                    Elements cells = row.select("td");
//                    Map<String, Object> rowData = new HashMap<>();
//                    for (int i = 0; i < cells.size() && i < headerList.size(); i++) {
//                        String headerText = headerList.get(i);
//                        String cellText = cells.get(i).text();
//                        rowData.put(headerText, cellText);
//                    }
//                    result.add(rowData);
//                }
//            } else {
//                System.out.println("未找到表格");
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//
//        return result;
//    }
public static List<Map<String, Object>> getRenRZuBillList(String authorization,String orderId,String  cookie) {
    List<Map<String, Object>> result = new ArrayList<>();
    List<String> firstHeaderList = new ArrayList<>(); //保存第一页的表头
    try {
        //获取第一页数据
        firstHeaderList = getPageData(orderId, 1, cookie, result, null);
        //System.out.println("成功获取到了第一页 ~ "+firstHeaderList);
        //获取总期数
        Map<String, Object> firstRow = result.get(0);
        String zuqi = firstRow.get("期数").toString();
        String cleanZuqi = zuqi.replaceAll("[^0-9/]", "");
        int totalTrems = Integer.parseInt(cleanZuqi.split("/")[1]);
        System.out.println("总期数："+totalTrems);
        if (totalTrems > 30){
            getPageData(orderId, 2, cookie, result,firstHeaderList);
            getPageData(orderId, 3, cookie, result,firstHeaderList);
            System.out.println("总期数："+totalTrems+"大于30 ，有第三页");
        }else if (totalTrems > 15){
            getPageData(orderId, 2, cookie, result,firstHeaderList);
            //System.out.println("第2页"+getPageData(orderId, 2, cookie, result,firstHeaderList));
            System.out.println("总期数："+totalTrems+"大于15 ，有第二页");
        }else {
            System.out.println("获取第一页数据");
        }
    }catch (Exception e){
        e.printStackTrace();
    }
    return result;
}
    private static List<String> getPageData(String orderId, int page, String cookie, List<Map<String, Object>> result, List<String> firstHeaderList) {
        List<String> headerList = new ArrayList<>();
        try {
            String url = "https://admin.rrzu.com/v2-bill/index?order_id="+orderId + "&page="+page;
            String html = Tool.getRequest(url,"","", "", cookie);
            Document doc = Jsoup.parse(html);
            Element table = doc.selectFirst("table.table.table-bordered");
            if (table != null) {
                //获取当前实际表头(只在第一页获取)
                Elements headers = table.select("thead th");
                if (firstHeaderList == null){
                    for (Element header : headers) {
                        headerList.add(header.text());
                    }
                }else {
                    headerList = new ArrayList<>(firstHeaderList);
                }
                //处理表头和数据行
                Elements rows = table.select("tbody tr");
                for (Element row : rows) {
                    Elements cells = row.select("td");
                    Map<String, Object> rowData = new HashMap<>();
                    for (int i = 0; i < cells.size() && i < headerList.size(); i++) {
                        String headerText = headerList.get(i);
                        String cellText = cells.get(i).text();
                        rowData.put(headerText, cellText);
                    }
                    result.add(rowData);
                }
            }
            System.out.println("成功获取了第"+page+"页数据");
            return headerList;
        }catch (Exception e){
            e.printStackTrace();
        }
        return headerList;
    }

}